package com.yanqu.road.server.manger.activity.throwingPot;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ThrowingPot;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.logic.bussiness.activity.ThrowingPotBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.throwingPot.ThrowingPotModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ThrowingPotMgr extends TempMgr {

    public static ActivityInfo activityInfo = null;

    private static Map<Integer, ThrowingPot> throwingPotMap = new ConcurrentHashMap<>();

    private static RandomHelper random = new RandomHelper();

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void reloadActivityInfo(){
        loadActivity();
    }

    private static void loadActivity(){
        List<ActivityInfo> activityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.ThrowingPotActivity.getValue());
        ActivityInfo inTimeActivity = null;
        for (ActivityInfo ac : activityInfoList) {
            if (ac.getType() == eActivityType.ThrowingPotActivity.getValue()) {
                inTimeActivity = ac;
                break;
            }
        }
        if (inTimeActivity != null) {
            //加载活动配置
            loadActivityConfig(inTimeActivity);
        }
    }

    private static void loadActivityConfig(ActivityInfo ac){
        throwingPotMap = ThrowingPotBussiness.getThrowingPotConfigMap(ac.getActivityId());
        activityInfo = ac;
        List<GamePlayer> playerList = GamePlayerMgr.getCopyPlayerList();

        if (playerList != null && playerList.size() > 0) {
            playerList.forEach(action -> {
                action.getModule(ThrowingPotModule.class).syncActivity();
            });
        }
    }

    public static ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public static Map<Integer, ThrowingPot> getThrowingPotMap() {
        return throwingPotMap;
    }

    /**
     * 飞箭送礼参数
     * 需要消耗的道具 goodsId = num |银两系数1|银两系数2|箭袋数量1|箭袋数量2
     * @return
     */
    public static List<String> getActivityParam3(){
        return StringUtils.stringToStringList(activityInfo.getParam3(), "\\|");
    }

    /**
     * 领取飞箭送礼奖励
     * @param earnSpeed  领取奖励的玩家赚速
     * @return Property
     */
    public static Property getDisperseGoldReward(BigInteger earnSpeed) {
        List<String> param3List = getActivityParam3();
        //银两奖励
        int totalSilver = random.next(Integer.valueOf(param3List.get(1)), Integer.valueOf(param3List.get(2)));
        BigInteger silver = earnSpeed.multiply(BigInteger.valueOf(totalSilver));

        //随机箭袋奖励
        int candyNum = random.next(Integer.valueOf(param3List.get(3)), Integer.valueOf(param3List.get(4)));

        Property property = new Property();
        property.addProperty(GameConfig.GAME_MONEY_SILVER, silver);
        property.addProperty(eItemId.THROWING_POT_JIAN_DAI.getValue(), BigInteger.valueOf(candyNum));
        return property;
    }
}
